package com.test.utils;

import com.test.model.HouseMeterCurrentData;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Created by ZZF on 2017/6/29.
 */
public class HouseMeterCurrentGenerate {




    /**
     * 接收户表数据，并生成新的户表数据
     *
     * @param houseMeterCurrentData
     * @return
     */
    public static HouseMeterCurrentData generate(HouseMeterCurrentData houseMeterCurrentData, String houseFlag) {

        //获得上下限信息
        Map<String, String> map = BaseUtils.inputConfig();
        Date newDate = new Date();

        //得到初始化的工作时间
        String workTimeStr = map.get("WorkTime");

        //String转化为Date型
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        Date planTime = null;
        try {
            planTime = sdf.parse(workTimeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //得到时间差就是工作时间
        int workTime = (int) ((newDate.getTime() - planTime.getTime()) / (1000 * 60 * 60));

        //转化为BigDecimal类型
        BigDecimal timeBigDecimal = new BigDecimal(workTime);


        //得到瞬时流量的上下限
        Double house_flow_max = Double.valueOf(map.get("House_Flow_Max"));
        Double house_flow_min = Double.valueOf(map.get("House_Flow_Min"));
        //得到供水温度的上下限
        Double house_supplyTemperature_max = Double.valueOf(map.get("House_SupplyTemperature_Max"));
        Double house_supplyTemperature_min = Double.valueOf(map.get("House_SupplyTemperature_Min"));
        //得到回水温度的上下限
        Double house_returnTemperature_max = Double.valueOf(map.get("House_ReturnTemperature_Max"));
        Double house_returnTemperature_min = Double.valueOf(map.get("House_ReturnTemperature_Min"));

        //保留两位小数
        DecimalFormat df = new DecimalFormat("#.00");

        //得到符合上下限的随机数
        double tempFlow = (house_flow_min + Math.random() * (house_flow_max - house_flow_min));

        double tempSupplyTemperature = (house_supplyTemperature_min + Math.random() *
                (house_supplyTemperature_max - house_supplyTemperature_min));

        double tempReturnTemperature = (house_returnTemperature_min + Math.random() *
                (tempSupplyTemperature - house_returnTemperature_min));

        //得到新的瞬时流量
        BigDecimal newFlow = BaseUtils.getBigDecimal(df.format(tempFlow));

        BigDecimal zero = new BigDecimal(0.00);

        //得到新的供水温度
        BigDecimal newSupplyTemperature = BaseUtils.getBigDecimal(df.format(tempSupplyTemperature));

        //得到新的回水温度
        BigDecimal newReturnTemperature = BaseUtils.getBigDecimal(df.format(tempReturnTemperature));

        //公式系数
        BigDecimal formula = new BigDecimal(0);

        //得到新的瞬时热量（未完成版）
        BigDecimal newHeat = (newSupplyTemperature.subtract(newReturnTemperature)).multiply(newFlow);

        //得到新的累计流量
        BigDecimal newAccFlow = newAccFlow(houseMeterCurrentData.getAccFlow(), houseMeterCurrentData.getFlow(),
                newFlow, houseMeterCurrentData.getPlanTime());
        //得到新的累计热量
        BigDecimal newAccHeat = newAccFlow(houseMeterCurrentData.getAccHeat(), houseMeterCurrentData.getHeat(),
                newHeat, houseMeterCurrentData.getPlanTime());

        if (!"0".equals(houseFlag)) {
            houseMeterCurrentData.setFlow(newFlow);

            houseMeterCurrentData.setAccFlow(newAccFlow);

            houseMeterCurrentData.setHeat(newHeat);

            houseMeterCurrentData.setAccHeat(newAccHeat);
        } else {
            houseMeterCurrentData.setFlow(zero);

//            houseMeterCurrentData.setAccFlow(zero);

            houseMeterCurrentData.setHeat(zero);

//            houseMeterCurrentData.setAccHeat(zero);
        }

        houseMeterCurrentData.setReturnTemperature(newReturnTemperature);

        houseMeterCurrentData.setSupplyTemperature(newSupplyTemperature);

        houseMeterCurrentData.setPlanTime(new Date());

        houseMeterCurrentData.setWorkTime(timeBigDecimal);


        return houseMeterCurrentData;
    }


    /**
     * 求的当前的累计流量
     *
     * @param last
     * @param amount
     * @param planTime
     * @return
     */
    public static BigDecimal newAccFlow(BigDecimal lastAcc, BigDecimal last, BigDecimal amount, Date planTime) {


        if (lastAcc == null) {
            lastAcc = new BigDecimal(0);
        }
        if (last == null) {
            last = new BigDecimal(0);
        }


        BigDecimal countFlow = last.add(amount);  //上次的瞬时量+当前的瞬时量

        BigDecimal two = new BigDecimal(2);

        BigDecimal average = countFlow.divide(two, 2, RoundingMode.HALF_UP);//用两个量之和求的平均值，保留两位小数

        Date newDate = new Date();
        double workTime = 1;
        if (planTime != null) {
            workTime = (double) (newDate.getTime() - planTime.getTime())
                    / (1000 * 60 * 60); //用当前时间减去上次的任务时间，得到时间差值
        }
        workTime = BaseUtils.formatDouble(workTime);

        BigDecimal timeSub = new BigDecimal(workTime);

        BigDecimal currentFlow = average.multiply(timeSub);//平均的瞬时量*时间差，得到了当前的累计量

        return currentFlow.add(lastAcc); //把上次的累计量和当前的累计量相叠加
    }

}
